local tblGrpList = {}
local strGrpList = {}
local currentGroup = ""
local tblBuffer = {}

local configlist = 1 --just so we have access to this throughout this script


local function ConfigDialog(btn)
	local strConfig = ""
	if btn then
		strConfig = tblGrpList[currentGroup][btn.DBID]
	end
	local window = vgui.Create( "DFrame" )
		window:SetSize( 600, 150 )
		window:Center()
		window:SetTitle( "Config Dialog" )
		window:SetVisible( true )
		window:MakePopup()
		
	local TextEntry = vgui.Create("DTextEntry", window)
		TextEntry:SetText( strConfig )
		TextEntry:SetSize( 500, 20 )
		TextEntry:SetPos( 80, 50 )
	
	local label = vgui.Create("DLabel", window)
		label:SetSize(50, 30)
		label:SetText("Config:")
		label:SetPos(30, 50)
		
	--Next lets make ze buttons!!!!
	local button = vgui.Create( "DButton", window )
		button:SetSize( 90, 30 )
		button:SetPos( 10, 100)
		button:SetText("Add")
	if btn.DBID then
		button.DoClick = function()
							tblBuffer.data = TextEntry:GetValue()
							tblBuffer.group = currentGroup
							LocalPlayer():ConCommand("netadmin_mod_cfg " .. btn.DBID .. " \"" .. netadmin.Lib.MakeQuotesSafe(TextEntry:GetValue()) .. "\"")
							window:Close() 
							end
	else
		button.DoClick = function()
							tblBuffer.data = TextEntry:GetValue()
							tblBuffer.group = currentGroup
							LocalPlayer():ConCommand("netadmin_add_cfg " .. currentGroup .. " \"" .. netadmin.Lib.MakeQuotesSafe(TextEntry:GetValue()) .. "\"")
							window:Close()
							end
	end
		
		--Next lets make ze buttons!!!!
	local button = vgui.Create( "DButton", window )
		button:SetSize( 90, 30 )
		button:SetPos( 500, 100)
		button:SetText("Cancel")
		button.DoClick = function() window:Close() end
end

local function DelConfig(btn)
	LocalPlayer():ConCommand("netadmin_del_cfg " .. currentGroup .. " " .. btn.DBID)
	tblGrpList[currentGroup][btn.DBID] = nil
	LoadConfigList( tblGrpList[currentGroup] ) -- refreshes the list
end

function netadmin.OpenConfigMenu()
	--1st we need to set the main window
	local w = 800
	local h = 600
	local window = vgui.Create( "DFrame" )
	window:SetSize( w, h )
	window:Center()
	window:SetTitle( "Group Configuration" )
	window:SetVisible( true )
	window:MakePopup()
	
	--Labels
	local label = vgui.Create("DLabel", window)
		label:SetSize(100, 30)
		label:SetText("Groups")
		label:SetPos(25, 20)
		
	local label = vgui.Create("DLabel", window)
		label:SetSize(100, 30)
		label:SetText("Configs")
		label:SetPos(425, 20)
	
	
	--Next lets make the parent panel for the group list
	local grouplist = vgui.Create( "DPanelList", window )
		grouplist:EnableVerticalScrollbar()
		grouplist:EnableHorizontal( false )
		grouplist:SetSize( w - 650, h - 100 )
		grouplist:SetPos( 10, 50 )
		grouplist:AddItem(LoadGroupList( tblGrpList))

	--Next lets make the config parent panel
		configlist = vgui.Create( "DPanelList", window )
		configlist:EnableVerticalScrollbar()
		configlist:EnableHorizontal( false )
		configlist:SetSize( w - 200, h - 100 )
		configlist:SetPos( 175, 50 )
	
	--Next lets make ze buttons!!!!
	local button = vgui.Create( "DButton", window )
		button:SetSize( 90, 30 )
		button:SetPos( 320, 560)
		button:SetText("Close")
		button.DoClick = function() window:Close() end
	
	local button = vgui.Create( "DButton", window )
		button:SetSize( 90, 30 )
		button:SetPos( 650, 560)
		button:SetText("Add Config Item")
		button.DoClick = ConfigDialog
		
	local button = vgui.Create( "DButton", window )
		button:SetSize( 90, 30 )
		button:SetPos( 30, 560)
		button:SetText("Add Group")
		button.DoClick = function() window:Close() end
end




function LoadConfigList( tblConfigs )
	configlist:Clear() --clear it 1st
	local width = configlist:GetWide()
	for i, v in pairs(tblConfigs) do
		configlist:AddItem(AddConfig(v, width, i))
	end
end

function AddConfig( strLine, intPanelSize, strDBID )
	local panel = vgui.Create("Panel")
		panel:SetSize(intPanelSize - 5, 20)
	
	local label = vgui.Create("DLabel", panel)
		label:SetSize(intPanelSize - 100, 20)
		label:SetText(strLine)
	
	local Button = vgui.Create( "DImageButton", panel)
		Button:SetImage( "gui/silkicons/wrench" )
		Button:SetSize( 16, 16 )
		Button:SetPos( intPanelSize - 75 , 0)
		Button:SetDrawBorder( true )
		Button.DBID = strDBID
		Button.DoClick = ConfigDialog
	
	local Button = vgui.Create( "DImageButton", panel)
		Button:SetImage( "gui/silkicons/check_off" )
		Button:SetSize( 16, 16 )
		Button:SetPos( intPanelSize - 50 , 0)
		Button:SetDrawBorder( true )
		Button.DBID = strDBID
		Button.DoClick = DelConfig
	return panel
end


function SetGroup(btn)
	LoadConfigList(tblGrpList[btn.data])
	currentGroup = btn.data
end

function LoadGroupList( tblGroupList )
	local spacer = 5
	local increment = 0
	local intNumGroups = table.Count(tblGroupList)
	local panel = vgui.Create("Panel")
		panel:SetSize(150, intNumGroups * (15 + spacer + 5))
	for k, v in pairs(tblGroupList) do
		local button = vgui.Create("RadioButton", panel)
			button:SetSize(205, 15)
			button:SetPos(5, increment * (15 + spacer))
			button:SetText(tostring(k))
			button.data = k
			button:SetActionFunction(SetGroup)
			increment = increment + 1
	end
	
	return panel
end
--concommand.Add( "thedermatest", Openmenu )

local function StartConfigs()
	strGrpList = ""
end
usermessage.Hook( "netadminStartConfigs", StartConfigs )

local function RcvConfigs(um)
	strGrpList = strGrpList .. um:ReadString()
end
usermessage.Hook( "netadminConfigs", RcvConfigs )

local function EndConfigs()
	tblGrpList = ULib.parseKeyValues(strGrpList, true)
	for i, v in pairs(tblGrpList) do
		tblGrpList[i] = netadmin.Lib.MakeStringTableRevert(v)
	end
	
	netadmin.OpenConfigMenu()
end
usermessage.Hook( "netadminEndConfigs", EndConfigs )

local function FlushBuffer(um)
	tblGrpList[tblBuffer.group][um:ReadString()] = tblBuffer.data
	tblBuffer = {}
	LoadConfigList(tblGrpList[currentGroup])
end
usermessage.Hook( "netadminFlushConfigBuffer", FlushBuffer )



